package com.heima.kafka.boot.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 消费者发送消息的类
 */
@RestController
@RequestMapping("/send")
public class ProducerController {

    @Resource
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 发送消息的方法
     * @return
     */
    @GetMapping
    public String sendMessage(){
        /*Map map=new HashMap<>();
        map.put("name","张三");
        kafkaTemplate.send("boot-topic", JSON.toJSONString(map));*/


        for (int i = 0; i < 5; i++) {
            if(i%2==0){
                kafkaTemplate.send("boot-kafkastream-topic1","hello boot kafka");
            }else{
                kafkaTemplate.send("boot-kafkastream-topic1","hello boot itcast");
            }
        }
        return "发送成功";
    }
}
